
#include "main.h"


typedef enum
{
    DI1_CH=0,
    DI2_CH,
    DI3_CH,
    DI4_CH,
    DI5_CH,
    DI6_CH,
    DI7_CH,
    DI8_CH,
    DI9_CH,
    DI10_CH,
    DI_MAX_CH,
}Ecu_DI_Ch;

typedef struct
{
    Ecu_DI_Ch ch; /*继电器通道*/
    u8 port;            /*端口编号*/
    GPIO_Type *base;    /*端口地址*/
    u32 pin;            /*Relay to MCU IO pin*/
    u8 sts;             /*继电器状态*/
}Ecu_DI_Conf;

Ecu_DI_Conf DIConf[DI_MAX_CH]={
    {DI1_CH,Bsw_Port_E,PTE,PIN9_IDX},       /*PTE9 As DI1*/
    {DI2_CH,Bsw_Port_D,PTD,PIN13_IDX},      /*PTD13 As DI2*/
    {DI3_CH,Bsw_Port_B,PTB,PIN5_IDX},       /*PTB5 As DI3*/
    {DI4_CH,Bsw_Port_D,PTD,PIN7_IDX},       /*PTD7 As DI4*/
    {DI5_CH,Bsw_Port_D,PTE,PIN14_IDX},      /*PTD14 As DI5*/
    {DI6_CH,Bsw_Port_E,PTE,PIN8_IDX},       /*PTE8 As DI6*/
    {DI7_CH,Bsw_Port_B,PTB,PIN17_IDX},      /*PTB4 As DI7*/
    {DI8_CH,Bsw_Port_D,PTD,PIN6_IDX},       /*PTD6 As DI8*/
    {DI9_CH,Bsw_Port_C,PTC,PIN13_IDX},      /*PTC13 As DI9*/
    {DI10_CH,Bsw_Port_C,PTC,PIN12_IDX},     /*PTC12 As DI10*/
};


void ECU_GPIO_INIT(void)
{
    //初始化Relay使能脚参数
    for(Ecu_DI_Ch ch=DI1_CH;ch<DI_MAX_CH;ch++)
    {
        BSW_GPIO_MUX_CONFIG(DIConf[ch].port,DIConf[ch].pin,PORT_MUX_AS_GPIO);
        BSW_GPIO_Direction(DIConf[ch].base,DIConf[ch].pin,0);//输入
        
        BSW_GPIO_IRQ_CONFIG(DIConf[ch].port,DIConf[ch].pin,PORT_INT_EITHER_EDGE);
    }

    INT_SYS_EnableIRQ(PORTB_IRQn);
    INT_SYS_EnableIRQ(PORTC_IRQn);
    INT_SYS_EnableIRQ(PORTD_IRQn);
    INT_SYS_EnableIRQ(PORTE_IRQn);
}

inline void DIIO_IRQ(PORT_Type *port_base)
{
    GPIO_Type *base;

    if(port_base==PORTA)
        base=PTA;
    else if(port_base==PORTB)
        base=PTB;
    else if(port_base==PORTC)
        base=PTC;
    else if(port_base==PORTD)
        base=PTD;
    else if(port_base==PORTE)
        base=PTE;

    for(Ecu_DI_Ch i=DI1_CH;i<DI_MAX_CH;i++)
    {
        if(DIConf[i].base==base)
        {
            DIConf[i].sts=BSW_GPIO_ReadBit(base,DIConf[i].pin);
            PINS_DRV_ClearPinIntFlagCmd(port_base,DIConf[i].pin);
        }
    }
}



